<?php
namespace app\common\model;
use think\Config;
use think\Model;

class Like extends Model{

    protected $table = "jiujiu_product_user_like";

    public function userIsLikeProduct($uid,$productId,$shopId){
       return $this->where('uid',$uid)->where('shopId',$shopId)->where('productId',$productId)->count();
    }


    public function cancelLike($productId,$shopId,$uid){

       return $this->where('uid',$uid)->where('shopId',$shopId)->where('productId',$productId)->delete();
    }

    public function addLike($productId,$shopId,$uid){

        return $this->insertGetId([
            'uid'=>$uid,
            'shopId'=>$shopId,
            'productId'=>$productId,
            'createTime'=>time(),
        ]);
    }


    public function addOrCancelLike($productId,$shopId,$uid){
        $like =  $this->where('uid',$uid)->where('shopId',$shopId)->where('productId',$productId)->find();
        if($like){
            $this->cancelLike($productId,$shopId,$uid);
            return 1;
        }
        else{
            $this->addLike($productId,$shopId,$uid);
            return 2;
        }

    }

}